package com.xuhuan.example.example015;

/**
 * @author: 徐欢
 * @date: 2023/11/12 18:35
 * @description:
 */
public class Example015 {

    public static void main(String[] args) {
        // 定义一个key，用来做加密解密的key
        int key = 817;
        // 字符串
        String str = "今天是个好日子！！！";
        String exclusiveOr = exclusiveOr(str, key);
        String exclusiveOrTwo = exclusiveOr(exclusiveOr, key);
    }

    /**
     * 异或运算
     * 如果某个字符（或数值）x与一个数组m进行异或运算得到y，则再用y与m进行异或运算就可以还原为x
     *
     * @param str
     * @param key
     * @return
     */
    public static String exclusiveOr(String str, int key) {
        char[] chars = str.toCharArray();
        System.out.println("key：" + key);
        System.out.println("异或运算前字符串：" + str);
        for (int i = 0; i < chars.length; i++) {
            chars[i] = (char) (chars[i] ^ key);
        }
        String result = new String(chars);
        System.out.println("异或运算后字符串：" + result);
        return result;
    }
}
